package cn.stylefeng.guns.modular.rm.service;

import cn.stylefeng.guns.modular.rm.entity.FbGameScore;
import cn.stylefeng.guns.modular.rm.entity.RmGameScore;
import cn.stylefeng.guns.modular.rm.mapper.FbGameScoreMapper;
import cn.stylefeng.guns.modular.rm.mapper.RmGameScoreMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/4/17 16:45
 **/
@Service
public class FbGameScoreService extends ServiceImpl<FbGameScoreMapper, FbGameScore> {
    @Autowired
    private FbGameScoreMapper fbGameScoreMapper;
    @Lazy
    @Autowired
    private FbGameService fbGameService;

    public List<String> getGameScoreIds() {
        List<FbGameScore> list = fbGameScoreMapper.selectList(null);
        return list.stream().map(FbGameScore::getFkRmGameId).collect(Collectors.toList());
    }

    public String getGameInfo() {
        List<String> oldGameids = getGameScoreIds();
        List<String> ids = fbGameService.getGameids(new Date());

        for (int i = 0; i < ids.size(); i++) {
            List<FbGameScore> lists = new ArrayList<>();
            if (oldGameids.contains(ids.get(i))) {
                continue;
            }
            String gameid = ids.get(i);
            JSONObject body = fbGameService.getGames("1", gameid, null);
            JSONArray mgs = body.getJSONObject("data").getJSONArray("mg");
            for (int j = 0; j < mgs.size(); j++) {
                JSONObject ms = mgs.getJSONObject(j);
                if (Arrays.asList("波胆", "波胆-上半场").contains(ms.getString("nm"))) {
                    String type = null;
                    switch (ms.getString("nm")) {
                        case "波胆":
                            type = "1";
                            break;
                        case "波胆-上半场":
                            type = "2";
                            break;
                    }
                    JSONArray mks = ms.getJSONArray("mks");
                    for (int k = 0; k < mks.size(); k++) {
                        JSONObject mk = mks.getJSONObject(k);
                        JSONArray ops = mk.getJSONArray("op");
                        for (int l = 0; l < ops.size(); l++) {
                            JSONObject op = ops.getJSONObject(l);
                            String na = op.getString("na"); //比分
                            String od = op.getString("od"); //赔率
                            FbGameScore build = FbGameScore.builder()
                                    .fkRmGameId(ids.get(i))
                                    .score(na)
                                    .rate(od)
                                    .type(type)
                                    .build();
                            lists.add(build);
                        }
                    }
                }

            }
            this.saveBatch(lists);
        }
        return null;
    }
}
